#include "Utils.h"
#include <cstdio>
#include <sys/stat.h>
#include <iostream>
#include <QMessageBox>
#include "FileNode.h"
/*---------------------------------------------------------------------------*/
using namespace std;
/*---------------------------------------------------------------------------*/
// Convert a KiB size into  xxx XiB
string Utils::formatSize( double kib, char baselevel )
{
	bool bFormated = false;
	char level = baselevel;
	
	// simplify
	while( !bFormated )
	{
		if( kib > 1024 )
		{
			kib /= 1024;
			level++;			
		}	
		
		if( kib < 0 )
		{
			kib *= 1024;
			level--;			
		}			
		
		if( kib <= 1024 && kib >= 0 )
		{
			bFormated = true;
		}		
			
	}


	// get correct unit
	string unit;
	switch(level)
	{
		case 4: unit = "TiB"; break;
		case 3: unit = "GiB"; break;
		case 2: unit = "MiB"; break;
		case 1: unit = "KiB"; break;
		case 0: unit = "B"; break;
	}
	
	// format
	char buff[128];
	if( !level )
		sprintf(buff,"%i %s", (int)kib, unit.c_str());
	else
		sprintf(buff,"%.2f %s", kib, unit.c_str());
	
	
	// return
	string ret = buff;
	return ret;
}
/*---------------------------------------------------------------------------*/
string Utils::toString( uint val)
{
	string ret;
	char buff[128];
	sprintf(buff,"%u", val);
	ret = buff;
	return ret;
}
/*---------------------------------------------------------------------------*/
string Utils::toString( int val)
{
	string ret;
	char buff[128];
	sprintf(buff,"%i", val);
	ret = buff;
	return ret;
}
/*---------------------------------------------------------------------------*/
string Utils::getExt(string name)
{
	string ret = "";
	int i;
	i = name.find_last_of('.');
	if( i < 1 ) return ret;
	ret = name.substr(i+1,name.length() - i - 1 );
	for(i=0; i< ret.length(); i++)
	{
		if( ret[i] >= 'A' && ret[i] <= 'Z' ) ret[i] -= 'A' - 'a';
	}
	return ret;
}
/*---------------------------------------------------------------------------*/
char Utils::getLastChar(string name)
{
	return name[name.length()-1];
}
/*---------------------------------------------------------------------------*/
string Utils::escapeChars(string str)
{
	string ret = str;
	int i;
	int decal = 0;
	
	string chars = " ()\"\'[]\\&!";
	
	for(i=0; i< str.length(); i++)
	{	
		if( chars.find( str[i] ) != string::npos )	
		{
			ret.insert(i + decal, "\\");
			decal++;
		}
	}
	
	return ret;
}
/*---------------------------------------------------------------------------*/
string Utils::groupStringFiles( vector<FileNode*> v, char ch, bool escchars )
{
	string str = "";
	
	foreach(FileNode*, file, v)
	{
		string temp = (*file)->getAbsolutePath();
		if( escchars ) temp = Utils::escapeChars( temp );
		str += temp.c_str();
		str += ch;
	}
	
	return str;
}
/*---------------------------------------------------------------------------*/
bool Utils::isDirectory(const string dname)
{
	struct stat sbuf;

	if (lstat(dname.c_str(), &sbuf) == -1) 
	{

		return false;
	}

	if(S_ISDIR(sbuf.st_mode)) 
	{
		return true;
	}

	return false;

}

/*---------------------------------------------------------------------------*/

